package com.js.service.impl;

import com.js.entity.DClass;
import com.js.dao.DClassDao;
import com.js.service.DClassService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (DClass)表服务实现类
 *
 * @author makejava
 * @since 2022-06-06 22:55:59
 */
@Service("dClassService")
@Transactional
public class DClassServiceImpl implements DClassService {
    @Resource
    private DClassDao dClassDao;

    /**
     * 通过ID查询单条数据
     *
     * @param cId 主键
     * @return 实例对象
     */
    @Override
    public DClass queryById(Integer cId) {
        return this.dClassDao.queryById(cId);
    }

    /**
     * 分页查询
     *
     * @param dClass 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<DClass> queryByPage(DClass dClass, PageRequest pageRequest) {
        long total = this.dClassDao.count(dClass);
        return new PageImpl<>(this.dClassDao.queryAllByLimit(dClass, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param dClass 实例对象
     * @return 实例对象
     */
    @Override
    public DClass insert(DClass dClass) {
        this.dClassDao.insert(dClass);
        return dClass;
    }

    /**
     * 修改数据
     *
     * @param dClass 实例对象
     * @return 实例对象
     */
    @Override
    public DClass update(DClass dClass) {
        this.dClassDao.update(dClass);
        return this.queryById(dClass.getCId());
    }

    /**
     * 通过主键删除数据
     *
     * @param cId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer cId) {
        return this.dClassDao.deleteById(cId) > 0;
    }

    @Override
    public List<DClass> queryAll() {
        return dClassDao.queryAll();
    }
}
